package com.example.badyloans.servlet;

import com.example.badyloans.common.ReturnResult;
import com.example.badyloans.config.CalculationLiXIJIS;
import com.example.badyloans.dao.TUserAccountMapper;
import com.example.badyloans.dao.TUserWalletMapper;
import com.example.badyloans.pojo.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.example.badyloans.dao.TBorrowMapper;
import com.example.badyloans.servlet.TBorrowService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class TBorrowServiceImpl implements TBorrowService{

    @Resource
    private TBorrowMapper tBorrowMapper;
    @Resource
    private TUserAccountMapper tUserAccountMapper;
    @Resource
    private TUserWalletMapper tUserWalletMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return tBorrowMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(TBorrow record) {
        return tBorrowMapper.insertSelective(record);
    }

    @Override
    public TBorrow selectByPrimaryKey(String id) {
        return tBorrowMapper.selectByPrimaryKey(id);
    }

    @Override
    public int sum() {
        return tBorrowMapper.sum();
    }

    @Override
    public List<TBorrow> selectByPrimaryKeys(String id) {
        return tBorrowMapper.selectByPrimaryKeys(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TBorrow record) {
        return tBorrowMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int count(Integer[]borrowStates,String beginDate,String endDate) {
        return tBorrowMapper.count(borrowStates, beginDate, endDate);
    }

    @Override
    public List<TBorrow> selectfenye(Integer [] borrowStates, Integer currentPageNo,
                                     Integer pageSize, String beginDate, String endDate) {
        return tBorrowMapper.selectfenye(borrowStates, currentPageNo, pageSize, beginDate, endDate);

    }
    @Override
    public int update(String borrowId, Integer borrowState) {
        return tBorrowMapper.update(borrowId,borrowState);
    }

    @Override
    public boolean addBorrow(TBorrow tBorrow) {
        //1:判断是否完成个人资料
        TUserAccount tUserAccount = tUserAccountMapper.selectByPrimaryKey(tBorrow.getBorrowUserId());
        if(tUserAccount==null){
            return false;
        }
        if(tUserAccount.getFillUserinfo().equals(0)){
            //ReturnResult.fail("该用户没有完成个人资料填写，不能申请借款");
            return false;
        }else if(tUserAccount.getFillUserinfo().equals(1)){
            //查询当前用户的资金账户
            TUserWallet tUserWallet = tUserWalletMapper.selectByPrimaryKey(tBorrow.getBorrowUserId());
            //2：判断借款金额是否小于或等于剩余s授信额度
            if(!(tBorrow.getBorrowAmount()<=tUserWallet.getCreditLine()
                    &&  tBorrow.getBorrowAmount()<=tUserWallet.getResidualCreditLine())){
                    return false;
            }
            Long ins=CalculationLiXIJIS.calculationtotal(tBorrow.getBorrowAmount(),tBorrow.getYearRate(),tBorrow.getRepaymentMonth(),tBorrow.getRepaymentType());
            //3：新增借款标的记录
            tBorrow.setId(UUID.randomUUID().toString().replace("-","").toLowerCase());
            tBorrow.setBorrowType(1);
            tBorrow.setBorrowState(10);
            tBorrow.setTotalInterest(ins);
            tBorrow.setCreateTime(LocalDateTime.now());
            tBorrow.setApplyTime(LocalDateTime.now());
            int count=tBorrowMapper.insertSelective(tBorrow);
            if (count>0) {
                //4：同步修改可用授信额度和剩余授信额度
                //拿到授信额度减去当前充值的额度
                tUserWallet.setCreditLine(tUserWallet.getCreditLine()-tBorrow.getBorrowAmount());
                tUserWallet.setResidualCreditLine(tUserWallet.getResidualCreditLine()-tBorrow.getBorrowAmount());
                int update=tUserWalletMapper.updateByPrimaryKeySelective(tUserWallet);
                if(update>0){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public List<TBorrow> selectliubiao() {
        return tBorrowMapper.selectliubiao();
    }
}
